package com.woniu.commentorder.console;

import com.hc.entity.Comment;
import com.hc.entity.Paper;
import com.hc.utils.ResponseEntity;
import com.hc.vo.Commentvo;
import com.woniu.commentorder.service.CommentService;
import com.woniu.commentorder.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("comment")
public class CommentConsole {
    @Autowired
    private CommentService commentService;
    @Autowired
    UserService userService;
    @PostMapping("/add")
    public String add(@RequestBody Comment comment){
      commentService.addComment(comment);
      return "添加的comment的id的"+comment.getId();
    }
    @PostMapping("/getbypid")
    public ResponseEntity<List<Commentvo>> get(@RequestBody Paper paper) {
        List<Comment> comments = commentService.getComByPid(paper);
        HashMap<Integer, Commentvo> commentvos = new HashMap<>();
        comments.forEach(r -> {
            Commentvo commentvo = new Commentvo();
            commentvo.setContent(r.getText());
            commentvo.setNickName(userService.getById(r.getUser().getId()).getName());
            commentvo.setLikeNum(r.getLikenum());
            if (r.getLikenum() >= 1) {
                commentvo.setHasLike(true);
            } else {
                commentvo.setHasLike(false);
            }
            commentvo.setCreateTime(new Date().toString());
            commentvo.setId(r.getId());
            commentvo.setAvatarUrl(userService.getById(r.getUser().getId()).getProfile());
            commentvo.setPaper_id(r.getPaper().getId());
            if (r.getFid() == null||r.getFid() == 0) {
                commentvo.setChildren(new ArrayList<>());
                commentvos.put(commentvo.getId(), commentvo);
            }
        });
        comments.forEach(r -> {
            Commentvo commentvo = new Commentvo();
            commentvo.setPaper_id(r.getPaper().getId());
            commentvo.setContent(r.getText());
            commentvo.setNickName(userService.getById(r.getUser().getId()).getName());
            commentvo.setLikeNum(r.getLikenum());
            if (r.getLikenum() >= 1) {
                commentvo.setHasLike(true);
            } else {
                commentvo.setHasLike(false);
            }
            commentvo.setCreateTime(new Date().toString());
            commentvo.setId(r.getId());
            commentvo.setAvatarUrl(userService.getById(r.getUser().getId()).getProfile());
            if (r.getFid() != null&&r.getFid() != 0) {
                commentvos.get(r.getFid()).getChildren().add(commentvo);
            }
        });
        ArrayList<Commentvo> commentvore = new ArrayList<>();
        Set<Map.Entry<Integer, Commentvo>> entries = commentvos.entrySet();
        for (HashMap.Entry<Integer, Commentvo> e : entries) {
            commentvore.add(e.getValue());
        }
        return new ResponseEntity(commentvore);
    }
    @PostMapping("/like")
    public String like(@RequestBody Comment comment){
        commentService.inform(comment.getId());
        return "点赞成功";

    }
}
